package com.liupengyuan.system.user.action;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.liupengyuan.framework.util.JSONOuter;
import com.liupengyuan.framework.web.query.IGenericPage;
import com.liupengyuan.framework.web.struts2.BaseAction;
import com.liupengyuan.system.user.model.User;
import com.liupengyuan.system.user.service.UserService;
import com.opensymphony.xwork2.ModelDriven;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction implements ModelDriven<User>{

	private static final long serialVersionUID = -5188457976857928890L;

	@Autowired
	private UserService userService;
	
	private User loguser;
	
	public String ids;
	
	private File file; //上传的文件
	private String fileFileName; //文件名称
	private String fileContentType; //文件类型
	
	public void page() throws UnsupportedEncodingException{
		IGenericPage<User> iPage = (IGenericPage<User>) userService.findPageBy(loguser, getPage(), getRows(), getSidx(), getSord());
		JSONOuter.writePage(getPrintWriter(), iPage);
	}

	public void getById(){
		User loguserObj = userService.getById(loguser.getId());
		JSONOuter.writeBeanMsg(getPrintWriter(), loguserObj);
	}
	public void save(){
		this.userService.saveOrUpdate(loguser);
		JSONOuter.writeBeanMsg(getPrintWriter(), loguser,"数据保存成功");
	}
	public void delete(){
		String msg = "";
		if(StringUtils.isNotEmpty(ids)){
			String[] _ids = ids.split(",");
			this.userService.deleteByIds(_ids);
			msg = "成功删除 " + _ids.length + " 条记录!!!";
		}else{
			msg = "请选择要删除的记录!!!";
		}
		JSONOuter.writeSuccessMsg(getPrintWriter(), msg);
	}
	


	public User getModel() {
		loguser = new User();
		return loguser;
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	
	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	
}
